package com.edu.controller;

import com.edu.bean.Book;
import com.edu.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;

    @DeleteMapping("/removeBook")
    public ModelAndView removeBook(int bookId) {
        ModelAndView mav = new ModelAndView();
        int i = bookService.removeBook(bookId);
        Page<Book> allBook = bookService.getAllBook(1, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook);
        mav.setViewName("../book.jsp");
        return mav;
    }

    @GetMapping("/firstPage")
    public ModelAndView firstPage() {
        ModelAndView mav = new ModelAndView();
        Page<Book> allBook = bookService.getAllBook(1, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook);
        mav.setViewName("../book.jsp");
        return mav;
    }

    @GetMapping("/lastPage")
    public ModelAndView lastPage() {
        ModelAndView mav = new ModelAndView();
        Page<Book> allBook = bookService.getAllBook(1, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        int navigateLastPage = bookPageInfo.getNavigateLastPage();
        Page<Book> allBook1 = bookService.getAllBook(navigateLastPage, 5);
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook1);
        mav.setViewName("../book.jsp");
        return mav;
    }

    @GetMapping("/toPage")
    public ModelAndView toPage(int pageNum) {
        ModelAndView mav = new ModelAndView();
        Page<Book> allBook = bookService.getAllBook(pageNum, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook);
        mav.setViewName("../book.jsp");
        return mav;
    }

    @GetMapping("/blurryAllBook")
    public ModelAndView blurryAllBook(String blurryName) {
        ModelAndView mav = new ModelAndView();
        Page<Book> allBook = bookService.getBookByNameBlurry(blurryName, 1, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook);
        mav.setViewName("../book.jsp");
        return mav;
    }
    @PostMapping("/addBook")
    public ModelAndView addBook(Book book){
        ModelAndView mav = new ModelAndView();
        bookService.addBook(book);
        Page<Book> allBook = bookService.getAllBook(1, 5);
        PageInfo<Book> bookPageInfo = allBook.toPageInfo();
        mav.addObject("bookPageInfo", bookPageInfo);
        mav.addObject("allBook", allBook);
        mav.setViewName("../book.jsp");
        return mav;
    }

    @GetMapping("/detailBook")
    public ModelAndView detailBook(int bookId){
        ModelAndView mav = new ModelAndView();
        Book book = bookService.getBookById(bookId);
        mav.addObject("book",book);
        mav.setViewName("../detailBook.jsp");
        return mav;
    }
}
